package com.gree.rpc.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.gree.commom.result.BaseResult;
import com.gree.rpc.service.SendMessageService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.stereotype.Component;

/**
 * 短信发送服务实现类
 * @since 1.0.0
 */
@Service(interfaceClass = SendMessageService.class)
@Component
public class SendMessageServiceImpl implements SendMessageService {

	/**
	 * 发送短信
	 * @param phoneNum
	 * @return
	 */
	@Override
	public BaseResult sendMessage(String phoneNum) {
		try{
			//实例化一个认证对象，获取签名串
			Credential cred = new Credential("AKID2Xnoa8iWKssfNrF5pKZv2ye5HEvMHBHT", "BnEPLdgT0IMCnAdsrb5NisnP2UmuqZ3N");
			//实例化HttpProfile对象，调用短信发送接口的url
			HttpProfile httpProfile = new HttpProfile();
			httpProfile.setEndpoint("sms.tencentcloudapi.com");
			//实例化ClientProfile对象
			ClientProfile clientProfile = new ClientProfile();
			clientProfile.setHttpProfile(httpProfile);
			//实例化sms客户端
			SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);
			/**
			 * 第一个参数：电话号码
			 * 第二个参数：模板id
			 * 第三个参数：签名内容
			 * 第四个参数：短信sdkappid
			 */
			String params = "{\"PhoneNumberSet\":[\"86"+phoneNum+"\"],\"TemplateID\":\"490150\",\"Sign\":\"ego\"," + "\"SmsSdkAppid\":\"1400291704\"}";
			//格式化json
			SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
			//发送请求
			SendSmsResponse resp = client.SendSms(req);
			if ("ok".equalsIgnoreCase(resp.getSendStatusSet()[0].getCode())){
				return BaseResult.success();
			}
			System.out.println(SendSmsRequest.toJsonString(resp));
		} catch (TencentCloudSDKException e) {
			System.out.println(e.toString());
		}
		return BaseResult.error();
	}
}